package com.meilingcloud.web.controller.system;

import com.meilingcloud.common.annotation.Log;
import com.meilingcloud.common.core.controller.BaseController;
import com.meilingcloud.common.core.domain.AjaxResult;
import com.meilingcloud.common.core.page.TableDataInfo;
import com.meilingcloud.common.enums.BusinessType;
import com.meilingcloud.common.utils.poi.ExcelUtil;
import com.meilingcloud.system.domain.SysAttach;
import com.meilingcloud.system.service.ISysAttachService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 附件 Controller
 *
 * @author chenl
 * @date 2021-03-15
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/attach")
public class SysAttachController extends BaseController {

    private final ISysAttachService iSysAttachService;

    /**
     * 查询附件 列表
     */
    @PreAuthorize("@ss.hasPermi('system:attach:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAttach sysAttach) {
        startPage();
        List<SysAttach> list = iSysAttachService.queryList(sysAttach);
        return getDataTable(list);
    }

    /**
     * 导出附件 列表
     */
    @PreAuthorize("@ss.hasPermi('system:attach:export')")
    @Log(title = "附件 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysAttach sysAttach) {
        List<SysAttach> list = iSysAttachService.queryList(sysAttach);
        ExcelUtil<SysAttach> util = new ExcelUtil<SysAttach>(SysAttach.class);
        return util.exportExcel(list, "attach");
    }

    /**
     * 获取附件 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:attach:query')")
    @GetMapping(value = "/{attachId}")
    public AjaxResult getInfo(@PathVariable("attachId") Long attachId) {
        return AjaxResult.success(iSysAttachService.getById(attachId));
    }

    /**
     * 新增附件
     */
    @PreAuthorize("@ss.hasPermi('system:attach:add')")
    @Log(title = "附件 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAttach sysAttach) {
        return toAjax(iSysAttachService.save(sysAttach));
    }

    /**
     * 修改附件
     */
    @PreAuthorize("@ss.hasPermi('system:attach:edit')")
    @Log(title = "附件 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAttach sysAttach) {
        return toAjax(iSysAttachService.updateById(sysAttach));
    }

    /**
     * 删除附件
     */
    @PreAuthorize("@ss.hasPermi('system:attach:remove')")
    @Log(title = "附件 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attachIds}")
    public AjaxResult remove(@PathVariable Long[] attachIds) {
        return toAjax(iSysAttachService.removeByIds(Arrays.asList(attachIds)));
    }
}
